import re

def match_words(wlist,sen):
    """
    : param wlist : list of word, in unicode
    : param sen : sentence, in unicode
    : return : [ { word , start , end } ]
    """
    if len(wlist) > 1000:
        #TODO build index
        todo = 1
    else:
        # iteration each word to find match
        winfo_list = []
        for w in wlist:
            for m in re.finditer(w,sen):
                winfo_list.append( (w,m.start(),m.end()))
        winfo_list.sort(lambda x,y:cmp(x[1],y[1]))
        #print winfo_list

        # remove dup and 
        while 1:
            found = False
            for i,w_item in enumerate(winfo_list):
                #for j in reversed(range(i+1,len(winfo_list))):
                for j in reversed(range(len(winfo_list))):
                    if i == j:
                        continue
                    if  winfo_list[i][1] <= winfo_list[j][1] and\
                            winfo_list[i][2] >= winfo_list[j][2]:
                        # remove the j
                        del winfo_list[j]
                        found = True
                        if found:
                            break
                if found:
                    break
            if not found:
                break

        #conflicting
        #print winfo_list,len(winfo_list)
        for i in reversed(range(1,len(winfo_list))):
            #print i
            if winfo_list[i][1] < winfo_list[i-1][2]:
                del winfo_list[i]

    return winfo_list
